
const InBrowser = typeof window !== "undefined";
const CookiesEnable = InBrowser && window.navigator.cookieEnabled;

const ApiCookiesUtil = {
  enable: CookiesEnable,
  save: function(key, value) {
    if (!key) {
      console.error('ApiCookies save key=nil')
      return false
    }
    if (!CookiesEnable) {
      console.error('ApiCookies save key=nil')
      return false
    }
    if (!value) value = ''
    if (typeof value === 'object') {
      value = JSON.stringify(value)
    }
    localStorage[key] = value
    window[key] = value
  },
  get: function(key, isObj = false) {
    if (!key) {
      console.error('ApiCookies get key=nil')
      return ''
    }
    if (!CookiesEnable) {
      console.error('ApiCookies get key=nil')
      return ''
    }
    let value = window[key]
    if (!value) {
      value = localStorage[key]
    }
    if (isObj) {
      value = JSON.parse(value)
    }
    return value
  }
}

export default ApiCookiesUtil